package cn.miketsu.iced.config;

/**
 * 数据库连接配置类
 *
 * @author wangzefeng
 * @since 1.0-SNAPSHOT
 */
public class DataBase {
    private String driver;
    private String url;
    private String username;
    private String password;

    public DataBase() {
    }

    public DataBase(String driver, String url, String username, String password) {
        this.driver = driver;
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 根据url自动配置driver
     * 目前仅支持Mysql
     *
     * @author wangzefeng
     * @since 1.1-SNAPSHOT
     */
    public void autoConfigDriver() {
        //Mysql
        if (this.getUrl().startsWith("jdbc:mysql://") && !this.getUrl().contains("serverTimezone=")) {
            this.setDriver("com.mysql.cj.jdbc.Driver");
            this.setUrl(this.getUrl() + (this.getUrl().contains("?") ? "&serverTimezone=Asia/Shanghai" : "?serverTimezone=Asia/Shanghai"));
        }
        //Mysql
        if (this.getUrl().startsWith("jdbc:mysql://") && this.getUrl().contains("serverTimezone=")) {
            this.setDriver("com.mysql.cj.jdbc.Driver");
        }
        //Oracle
        if (this.getUrl().startsWith("jdbc:oracle:thin:")) {
            this.setDriver("oracle.jdbc.driver.OracleDriver");
        }
        //DB2
        if (this.getUrl().startsWith("jdbc:db2://")) {
            this.setDriver("com.ibm.db2.jdbc.app.DB2Driver");
        }
    }
}

